﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NBFModel;
using PWMIS.DataMap.Entity;
using System.Collections;
using PWMIS.DataProvider.Data;
using PWMIS.DataProvider.Adapter;
using System.Data;

namespace NBFBLL
{
    /// <summary>
    /// 提醒设置
    /// </summary>
    public class RemindSetBLL
    {

        NBFDAL.SqlMapDAL.RemindSetDAL rsdal = new NBFDAL.SqlMapDAL.RemindSetDAL();


        #region  组合基金提醒

        /// <summary>
        /// 更新分行客户分组基金设置  保存组合提醒   删除在前台进行
        /// </summary>
        /// <param name="startday"></param>
        /// <param name="yingli"></param>
        /// <param name="kuisun"></param>
        /// <param name="userid"></param>
        /// <param name="zdid"></param>
        /// <returns></returns>
        public bool UpdateGroupFundSetting(string startday, string yingli, string kuisun, string userid, string zdid)
        {
            AdoHelper  db = MyDB.GetDBHelper();
            
            try
            {
                db.BeginTransaction();
                TB_zdgl_gz gz = new TB_zdgl_gz();
                OQL o = new OQL(gz);
                OQLCompare oc = new OQLCompare(gz);
                OQLCompare ocresult = (oc.Comparer(gz.UserID,OQLCompare.CompareType.Equal,userid) & oc.Comparer(gz.gzzdid,OQLCompare.CompareType.Equal,zdid));
                o.Delete().Where(ocresult);      
                int num =  EntityQuery<TB_zdgl_gz>.ExecuteOql(o,db);
                if (num > 0)
                {
                    TB_zdgl_gz gz1 = new TB_zdgl_gz();
                    gz1.ID = Guid.NewGuid().ToString();
                    gz1.lxID = "29";
                    gz1.UserID = userid;
                    gz1.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    gz1.StartDay = startday;

                    int yinglinum = 0;
                    int.TryParse(yingli, out yinglinum);
                    gz1.Yingli = yinglinum;

                    int kuisunnum = 0;
                    int.TryParse(kuisun, out kuisunnum);
                    gz1.Kuisun = kuisunnum;

                    gz1.State = "1";
                    gz1.gzzdid = zdid;

                    EntityQuery<TB_zdgl_gz> equery = new EntityQuery<TB_zdgl_gz>(gz1);
                    int num1 = equery.Insert(gz1, db);

                    db.Commit();
                    return true;
                }
                else
                {
                    db.Rollback();
                    return false;
                }
                    
            }
            catch (Exception ex)
            {
                db.Rollback();
                return false;
               
            }
            
        }

        #endregion




        #region 关注基金


        /// <summary>
        /// 获取单只基金提醒设置
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isFt"></param>
        /// <returns></returns>
        public DataTable getJJSetData(string userid, bool isFt)
        {
            DataTable dt = null;
            if (isFt)
            {
                dt = rsdal.GetRemindSetIsft(userid).Tables[0];
            }
            else
            {
                dt = rsdal.GetRemindSet(userid).Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 根据基金代码、用户编号、提醒类型修改跟踪设置表信息  修改 关注基金  如果是关注基金 先修改 再添加 如果是 单只基金提醒  直接添加
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <param name="jjdm">基金代码</param>
        /// <param name="yingli">盈利值</param>
        /// <param name="kuisun">亏损值</param>
        /// <param name="startDay">起始日期</param>
        /// <param name="tx_Type">提醒类型</param>
        /// <returns>返回修改是否成功</returns>
        public  bool UpdateGZByJJDM(string userid, string jjdm, string yingli, string kuisun, string startDay, string tx_Type, ArrayList txContent, string gzid)
        {
            AdoHelper  db = MyDB.GetDBHelper();
            try
            {
                db.BeginTransaction();
                TB_zdgl_gz gz = new TB_zdgl_gz();
                
                OQL o = new OQL(gz);
                //OQLCompare oc = new OQLCompare(gz);
                //OQLCompare ocp = (oc.Comparer(gz.ID, OQLCompare.CompareType.Equal,gzid));
                o.Update(gz.Yingli,gz.Kuisun,gz.StartDay,gz.AddTime).Where(o.Condition.AND(gz.ID,"",gzid));
                int num = EntityQuery<TB_zdgl_gz>.ExecuteOql(o, db);

                TB_zdgl_gz g1 = new TB_zdgl_gz();
                OQL o1 = new OQL(g1);
                OQLCompare oc1 = new OQLCompare(g1);
                OQLCompare ocp1 = (oc1.Comparer(g1.UserID, OQLCompare.CompareType.Equal, userid) & oc1.Comparer(g1.jjdm, OQLCompare.CompareType.Equal, jjdm) &
                    (oc1.Comparer(g1.lxID, OQLCompare.CompareType.Equal, "1") | (oc1.Comparer(g1.lxID, OQLCompare.CompareType.Equal, "2")) | oc1.Comparer(g1.lxID, OQLCompare.CompareType.Equal, "3"))
                    );
                o1.Delete().Where(ocp1);
                int num1 = EntityQuery<TB_zdgl_gz>.ExecuteOql(o1, db);

                

                for (int j = 0; j < txContent.Count; j++)
                {
                    TB_zdgl_gz g2 = new TB_zdgl_gz();
                    g2.ID = Guid.NewGuid().ToString();
                    g2.jjdm = jjdm;
                    g2.lxID = txContent[j].ToString();
                    g2.UserID = userid;
                    g2.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
                    g2.State = "1";
                    OQL o2 = new OQL(g2);
                    EntityQuery<TB_zdgl_gz> eq2 = new EntityQuery<TB_zdgl_gz>(g2);
                    int num2 = eq2.Insert(g2, db);
                }

                if (tx_Type == "26")
                {//单只基金盈亏
                    TB_zdgl_gz g3 = new TB_zdgl_gz();
                    OQL o3 = new OQL(g3);
                    o3.Select().Where(o3.Condition.AND(g3.ID, "", gzid));

                    List<TB_zdgl_gz> list = EntityQuery<TB_zdgl_gz>.QueryList(o3);
                    if (list.Count > 0)
                    {
                        //此方法未独立 此步骤不能完成
                        //conn.TranUpdate("Update TB_zdgl_gz set Nowshouyilv='" + CommonMethod.JJUtil.jisuanJJForShenzhen(openid, startDay) + "' where id='" + gzid + "'");
                    }

                }
                else
                { //客户关注的基金
                    //conn.TranUpdate("Update TB_zdgl_gz set Nowshouyilv='" + CommonMethod.JJUtil.getJJShouyilv(jjdm, startDay) + "',addtime='" + DateTime.Now.ToString("yyyy-MM-dd") + "' where id='" + gzid + "'");
                }
                db.Commit();

            }
            catch (Exception ex)
            {
                db.Rollback();
            }
            return false;
        }

        /// <summary>
        /// 添加/修改跟踪历史服务设置信息    添加保存关注基金   添加单只基金也用他
        /// </summary>
        ///<param name="yingli">盈利值</param>
        ///<param name="kuisun">亏损值</param>
        ///<param name="jjdm">基金代码集合</param>
        ///<param name="userid">用户编号</param>
        ///<param name="startday">起始日期</param>
        ///<param name="lxid">提醒类型</param>
        public bool InsertUpdateGZ(string yingli, string kuisun, ArrayList jjdm, string userid, string startday, ArrayList lxid, ArrayList txContent)
        {

            try
            {
                if (jjdm.Count > 0)
                {
                    List<TB_zdgl_gz> list = new List<TB_zdgl_gz>();
                    for (int i = 0; i < jjdm.Count; i++)
                    {
                        string nowshouyilv = "1";//获取最新收益率 等待公用方法开放

                        TB_zdgl_gz gz = new TB_zdgl_gz();
                        gz.ID = Guid.NewGuid().ToString();
                        gz.jjdm = jjdm[i].ToString();
                        gz.lxID = lxid[i].ToString();
                        gz.UserID = userid;
                        gz.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
                        gz.StartDay = startday;

                        int yinglinum = 0;
                        int.TryParse(yingli, out yinglinum);
                        gz.Yingli = yinglinum;

                        int kuisunnum = 0;
                        int.TryParse(kuisun, out kuisunnum);
                        gz.Kuisun = kuisunnum;

                        gz.State = "1";

                        decimal nowshouyilvdecimal = 0;
                        decimal.TryParse(nowshouyilv, out nowshouyilvdecimal);
                        gz.Nowshouyilv = nowshouyilvdecimal;

                        list.Add(gz);
                        //conn.TranUpdate("insert into TB_zdgl_gz (ID,jjdm,lxID,UserID,AddTime,startday,yingli,kuisun,state,nowshouyilv) values ('" + Guid.NewGuid().ToString() + "','" + jjdm[i].ToString() + "','" + lxid[i].ToString() + "','" + userid + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + startday + "','" + yingli + "','" + kuisun + "', '1'," + nowshouyilv + ")");
                        for (int j = 0; j < txContent.Count; j++)
                        {
                            TB_zdgl_gz gz1 = new TB_zdgl_gz();
                            gz1.ID = Guid.NewGuid().ToString();
                            gz1.jjdm = jjdm[i].ToString();
                            gz1.lxID = txContent[j].ToString();
                            gz1.UserID = userid;
                            gz1.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            gz1.State = "1";

                            list.Add(gz1);
                            //conn.TranUpdate("insert into TB_zdgl_gz (ID,jjdm,lxID,UserID,AddTime,state) values ('" + Guid.NewGuid().ToString() + "','" + jjdm[i].ToString() + "','" + txContent[j].ToString() + "','" + userid + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','1')");
                        }
                    }
                    int num = EntityQuery<TB_zdgl_gz>.Instance.Insert(list);
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除单只基金提醒设置详细   删除关注基金提醒
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="jjdm"></param>
        /// <returns></returns>
        public bool DeleteGZByID(string userid, string jjdm)
        {
            TB_zdgl_gz gz = new TB_zdgl_gz();
            OQL o = new OQL(gz);
            OQLCompare cmp = new OQLCompare(gz);
            OQLCompare cmpResult = (cmp.Comparer(gz.UserID, OQLCompare.CompareType.Equal, userid)
                & cmp.Comparer(gz.jjdm, OQLCompare.CompareType.Equal, jjdm) & (cmp.Comparer(gz.lxID, OQLCompare.CompareType.Equal, "1") | (cmp.Comparer(gz.lxID, OQLCompare.CompareType.Equal, "2"))
                | cmp.Comparer(gz.lxID, OQLCompare.CompareType.Equal, "28")
                ));

            o.Delete().Where(cmpResult);
            int num = EntityQuery<TB_zdgl_gz>.Instance.ExecuteOql(o);
            if (num > 0) return true;
            else return false;
        }

        /// <summary>
        /// 获取跟踪服务信息
        /// </summary>
        /// <param name="userID">用户编号</param>
        /// <param name="addtime">添加时间</param>
        public  DataTable GetGZByUserID(string userID)
        {
            return rsdal.GetGZByUserID(userID).Tables[0];
            //return conn.GetDataTable(sql);
        }

        #endregion





        /// <summary>
        /// 删除单只基金盈亏提醒设置
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="jjdm"></param>
        /// <returns></returns>
        public  bool deleteJJSetData(string userid, string jjdm)
        {
            //return conn.Update(@"delete from tb_zdgl_gz where userid = '" + userid + "' and jjdm = '" + jjdm + "'");
            TB_zdgl_gz g1 = new TB_zdgl_gz();
            OQL o = new OQL(g1);
            o.Delete().Where(o.Condition.AND(g1.UserID, "=", userid), o.Condition.AND(g1.jjdm, "=", jjdm));
            int num = EntityQuery<TB_zdgl_gz>.Instance.ExecuteOql(o);
            if (num > 0) return true;
            else return false;
        }



        #region 咨询操作
        /// <summary>
        /// 咨询订制
        /// </summary>
        /// <param name="arr">ID集合</param>
        /// <param name="UserID">用户ID</param>
        public void updateZX(ArrayList arr, string UserID)
        {//统一添加 再删除
            AdoHelper  db = MyDB.GetDBHelper();
            try
            {
                db.BeginTransaction();
                TB_zdgl_zx z = new TB_zdgl_zx();
                OQL o = new OQL(z);
                o.Delete().Where(o.Condition.AND(z.UserID, "", UserID));
                int num = EntityQuery<TB_zdgl_zx>.ExecuteOql(o, db);
                if (num > 0)
                {
                    for (int i = 0; i < arr.Count; i++)
                    {
                        TB_zdgl_zx zx = new TB_zdgl_zx();
                        zx.BookID = Guid.NewGuid().ToString();
                        zx.UserID = UserID;
                        zx.ModuleID = int.Parse(arr[i].ToString() != "" ? arr[i].ToString() : "0");
                        zx.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                        EntityQuery<TB_zdgl_zx> eq = new EntityQuery<TB_zdgl_zx>(zx);
                        int num1 = eq.Insert(zx, db);
                    }
                    db.Commit();
                }
                else
                {
                    db.Rollback();
                }

            }
            catch (Exception ex)
            {
                db.Rollback();
            }
        } 
        #endregion


        #region 提醒设置

        /// <summary>
        /// 添加提醒设置
        /// </summary>
        /// <param name="dm"></param>
        /// <returns></returns>
        public bool InsertTXSet(TB_zdgl_DayMyself dm)
        {
            int num = EntityQuery<TB_zdgl_DayMyself>.Instance.Insert(dm);
            if (num > 0) return true;
            else return false;
        }

        /// <summary>
        /// 修改提醒设置
        /// </summary>
        /// <param name="dm"></param>
        /// <returns></returns>
        public bool UpdateTXSet(TB_zdgl_DayMyself dm)
        {
            int num = EntityQuery<TB_zdgl_DayMyself>.Instance.Update(dm);
            if (num > 0) return true;
            else return false;
        }

        /// <summary>
        /// 删除咨询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteTXSet(List<string> ids)
        {
            try
            {
                foreach (string id in ids)
                {
                    TB_zdgl_DayMyself dm = new TB_zdgl_DayMyself();
                    dm.ID = id;
                    int num = EntityQuery<TB_zdgl_DayMyself>.Instance.Delete(dm);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion


        #region 组合基金提醒
        public  void UpdateZuheJJ(ArrayList Lx, int ZuheType, string userid)
        {
            AdoHelper db = MyDB.GetDBHelper();
            try
            {
                db.BeginTransaction();
                rsdal.CurrentDataBase = db;
                rsdal.DeleteZuheJJ(userid, ZuheType.ToString());

                List<TB_zdgl_gz> gzs = new List<TB_zdgl_gz>();
                for (int i = 0; i < Lx.Count; i++)
                {
                    TB_zdgl_gz gz = new TB_zdgl_gz();
                    gz.ID = Guid.NewGuid().ToString();
                    gz.ZuheType = ZuheType.ToString();
                    gz.lxID = Lx[i].ToString();
                    gz.UserID = userid;
                    gz.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    gzs.Add(gz);

                }
                int num = EntityQuery<TB_zdgl_gz>.Instance.Insert(gzs);
                db.Commit();
                //conn.TranBegin();
                //conn.TranDelete("TB_zdgl_gz", "UserID='" + userid + "' and lxID in (select id from TB_zdgl_gzlx where type=2) and ZuheType='" + ZuheType + "'");
                //for (int i = 0; i < Lx.Count; i++)
                //{
                //    conn.TranUpdate("insert into TB_zdgl_gz (ID,ZuheType,lxID,UserID,AddTime) values ('" + Guid.NewGuid().ToString() + "','" + ZuheType.ToString() + "','" + Lx[i].ToString() + "','" + userid + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')");
                //}
                //conn.TranCommit();
            }
            catch (Exception ex)
            {
                db.Rollback();
                //config.Debug(ex.ToString(), config.debugType.error);
            }
        }
        #endregion



    }
}
